// pages/orderDetail/orderDetail.js
import config from '../../../config.js';
var util = require('../../../utils/util.js');
const base = config;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: null,
    order: null,
    orderStatus: [],
    start: {
      address: '',
      Latitude: 0,
      Longitude: 0,
    },
    end: {
      address: '',
      Latitude: 0,
      Longitude: 0,
    },
  },
  openMap: function(e) {
    let lati = e.currentTarget.dataset.lati,
      long = e.currentTarget.dataset.long,
      address = e.currentTarget.dataset.address;
    console.log(lati, long, address);
    wx.openLocation({
      latitude: lati,
      longitude: long,
      scale: 18,
      address: address,
    })
  },
  contact: function() {
    wx.makePhoneCall({
      phoneNumber: this.data.order.order.account.tel,
    })
  },

  setStatus: function(e) {
    var that = this;
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;

    let arr = this.data.orderStatus;
    var status = arr[index];
    console.log(status);
    console.log(e);
    if (item.orderWay.isComplete == 1) {
      wx.showModal({
        title: "温馨提示",
        content: "是否确认完成该订单流程？",
        showCancel: true,
        confirmColor: "#137AF0",
        success: function(er) {
          console.log(er);
          if (er.confirm) {
            that.changeStatus(true);
          }
        }
      })
    } else if (item.orderWay.isGeneratePayItem == 1) {
      wx.navigateTo({
        url: './paymentItems?orderId=' + this.data.orderId + "&typeId=" + this.data.order.order.orderType.typeId,
      })
    } else {
      wx.showModal({
        title: "温馨提示",
        content: "是否确认当前订单最新状态？",
        showCancel: true,
        confirmColor: "#137AF0",
        success: function(er) {
          console.log(er);
          if (er.confirm) {
            that.changeStatus(false);
          }
        }
      })
    }
  },
  chooseMap: function(type) {
    var that = this;
    util.checkLocationIsOff(this)
    wx.chooseLocation({
      success: function(res) {
        console.log(res);
        if (type == 1) {
          that.setData({
            'start.address': res.address,
            'start.Latitude': res.latitude,
            'start.Longitude': res.longitude,

          })
        } else {
          that.setData({
            'end.address': res.address,
            'end.Latitude': res.latitude,
            'end.Longitude': res.longitude,

          })
        }

        //console.log(that.data)
        that.changeServiceLocation(type);
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var id = options.id;
    if (id != null) {
      this.setData({
        orderId: id
      })

      //订单追踪状态
      this.getTraceList();
    }

    this.loadData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    var isGoOn = wx.getStorageSync("isGoOn");
    if (isGoOn) {
      that.changeStatus(false);
    }
    that.listenOrderChange();
  },
  listenOrderChange: function() {
    var that = this;
    app.globalData.callback = function(res) {
      console.log('收到服务器推送订单消息内容：' + res.data);
      //解析消息 
      that.loadData();
      that.getTraceList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    wx.setStorageSync("isGoOn", false);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.setStorageSync("isGoOn", false);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  loadData() {
    var that = this;
    wx.showLoading({
      title: '数据正在加载中...',
    })
    //获取订单详情
    wx.request({
      url: base.orderDetailUrl,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: this.data.orderId,
        isKeeper: 1
      },
      method: "POST",
      success: function(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          that.setData({
            order: res.data.data
          })
        } else {
          wx.showToast({
            title: '订单获取失败！',
            icon: 'none',
            duration: 1500
          })
          return false;
        }
        wx.hideLoading();
      },
      fail: function() {
        that.loadData();
      }

    })
  },
  getTraceList() {
    var that = this;
    //获取订单详情
    wx.request({
      url: base.orderTraceList,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: this.data.orderId
      },
      method: "POST",
      success: function(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          if (res.data.data != null) {
            that.setData({
              orderStatus: res.data.data
            })
          }

        } else {
          wx.showToast({
            title: '订单获取失败！',
            icon: 'none',
            duration: 1500
          })
          return false;
        }
      }

    })
  },
  previewImg: function(e) {
    console.log(e.currentTarget.dataset.url);
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: url, //当前图片地址 
      urls: [url], //所有要预览的图片的地址集合 数组形式 
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  closeHandler: function() {
    this.setData({
      isShow: false
    })
  },
  changeStatus: function(flag) {
    var that = this;
    wx.showLoading({
      title: '正在操作',
    })
    var url = base.changeOrderStatus;
    if (flag) {
      url = base.orderComplete;
    }
    wx.request({
      url: url,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: that.data.orderId
      },
      success: function(res) {
        wx.hideLoading();
        console.log(res);
        if (res.data.status == 200) {
          wx.showToast({
            title: '状态设置完成',
          })
          that.getTraceList();
          that.loadData();
        } else {
          wx.showToast({
            title: "状态设置失败",
            icon: "none"
          })
        }
      }
    })
  },
  changeServiceLocation: function(type) {
    var that = this;
    wx.showLoading({
      title: '正在操作',
    })
    var location = JSON.stringify(that.data.start);
    if (type == 2) {
      location = JSON.stringify(that.data.end);
    }

    console.log(location);

    wx.request({
      url: base.changeOrderLocation,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        orderId: that.data.orderId,
        type: type,
        location: location
      },
      success: function(res) {
        wx.hideLoading();
        console.log(res);
        if (res.data.status == 200) {
          wx.showToast({
            title: '位置信息设置完成',
          })
          //that.changeStatus(false);
        } else {
          wx.showToast({
            title: "位置信息设置失败",
            icon: "none"
          })
        }
      }
    })
  },
  setLocation: function(e) {
    console.log(e);
    var index = e.currentTarget.dataset.index;
    this.chooseMap(index);
  },
  goToDinstination: function(e) {
    console.log(e);
    var index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../../map/mapTrace/mapTrace?type=' + index + "&orderId=" + this.data.orderId,
    })
  },
  backToDinstination: function(e) {
    console.log(e);
    var index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../../map/mapTrace/mapTrace?type=' + index + "&orderId=" + this.data.orderId,
    })
  },
  generatePriceAction: function() {
    wx.navigateTo({
      url: './paymentItems?type=1&orderId=' + this.data.orderId + "&typeId=" + this.data.order.order.orderType.typeId,
    })
  }
})